<template>
	<view class="content">
			观看权限：{{authType}}
			<view v-if="authType === 0">
				无密码登录
			</view>

			<view v-if="authType === 1">
				<view>
					<view class="spacing">
						<uni-easyinput prefixIcon="locked-filled" v-model="formPassword.watchPassword" placeholder="请输入观看密码" ></uni-easyinput>
					</view>					
				</view>				
			</view>
			
			<view v-if="authType === 2">
				<view class="spacing">
					<uni-easyinput prefixIcon="phone-filled" v-model="formPassword.name" placeholder="请输入手机号码" ></uni-easyinput>
				</view>
				<view class="spacing">
					<uni-easyinput prefixIcon="locked-filled" v-model="formPassword.watchPassword" placeholder="请输入图片验证" ></uni-easyinput>
				</view>
				<view class="spacing">
					<uni-easyinput prefixIcon="locked-filled" v-model="formPassword.watchPassword" placeholder="请输入验证码" ></uni-easyinput>
				</view>				
			</view>				

		
		<view class="btn-row">
			<button type="primary" class="login-btn" @tap="bindLogin">登录</button>
		</view>
		
	</view>
</template>

<script>
import defaultData from '@/common/js/default.js'
import { Request } from '@/common/js/util.js'

	export default {
		data() {
			return {
				authType: 0,
				watchPassword: '',
				roomInfo: {
					clientId: '',
					roomId: ''
				},
				formPassword: {
					name: 'ceshi',
					watchPassword: ''
				},
				formPhone: {
					name: '',
					watchPassword: ''
				},
				mobile: '',
				code: '',
				codeDuration: '',
				loginBtnLoading: '',
			}
		},
		onLoad() {
			this.roomInfo.clientId = '1440556408374566';
			this.roomInfo.roomId = '20000113';
			uni.setStorageSync('clientId', this.roomInfo.clientId);
			uni.setStorageSync('roomId', this.roomInfo.roomId);
			this.getConfig();
		},
		methods: {
			//获取直播间基础信息
			getConfig(){
				// 20000104 无密码登录 0
				// 20000103 密码登录 1
				// 20000101 手机验证码 2

				// 20000113 直播
				Request({
					url: defaultData.baseUrl +'/user/room/config/'+ this.roomInfo.roomId,
					method:'get',
				}).then(res=>{
					uni.showToast({
						icon:'success',
						title: res.data,
						duration: 2000
					});
					this.authType = res.data.authType;
					if(this.authType === 0){
						this.bindLogin()	

						setTimeout(()=>{
							uni.navigateTo({
								url: '/pages/live/live'
							})
						},1000)
					}
					// console.log(111,res.data.authType);
				}).catch(err=>{
					uni.hideLoading();
					uni.showToast({
						icon:'none',
						title: err.msg,
						duration: 2000
					});
				})
			},

			// 登录
			bindLogin(){
				let loginData,loginUrl='';
				// 无密码登录
				if(this.authType === 0){
					loginUrl = 'userLogin'
					loginData = {
						...this.roomInfo						
					}
				} 
				// 密码登录
				else if(this.authType === 1){
					loginUrl = 'userPwdLogin'
					loginData = {
						...this.formPassword,
						...this.roomInfo						
					}
				}
				// 手机号验证码登录
				else if(this.authType === 2){
					loginUrl = 'userMobileLogin'

				}
				Request({
					url: defaultData.baseUrl +'/user/' + loginUrl,
					method:'post',
					header: {
						'content-type': 'application/json' // 设置请求头为application/json
					},					
					data: loginData
				}).then(res=>{
					uni.showToast({
						icon:'success',
						title: res.data,
						duration: 2000
					});
					this.authType = res.data.authType;
					uni.setStorageSync('token', res.data.token);
					console.log(res.data.token);
				}).catch(err=>{
					uni.hideLoading();
					uni.showToast({
						icon:'none',
						title: err.msg,
						duration: 2000
					});
				})				
				console.log(111);
			},		
		},
		onReady() {
			
		}
	}
</script>

<style>

.input-custom{

}

.login-btn{
	background: #0274F7;
}

.spacing{
	margin: 20upx 0 20upx 0;
}
</style>
